package geeks.web;

import java.util.ArrayList;
import java.util.Collection;

import geeks.excepcion.DAOExcepcion;
import geeks.modelo.Persona;
import geeks.negocio.GestionPersonas;
import geeks.negocio.GestionRol;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ManttoUsuariosController {
	
	@RequestMapping(value = "/mttoUsuarios")	
	protected ModelAndView listarUsuarios(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		GestionPersonas neg = new GestionPersonas();
		Collection<Persona> lista = new ArrayList<Persona>();
		
		lista = neg.listarPersonas();
				
		return new ModelAndView("mantenerUsuarios", "LISTA", lista);
	}
	
	@RequestMapping(value = "/registrarUsuario", method = RequestMethod.POST)	
	protected ModelAndView registrarUsuario(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		Persona obj = new Persona();
		
		obj.setNombre(request.getParameter("txtNombre"));
		obj.setApePaterno(request.getParameter("txtApePaterno"));
		obj.setApeMaterno(request.getParameter("txtApeMaterno"));
		obj.setSexo(request.getParameter("rbnSexo"));
		obj.setTipoDocumento(request.getParameter("cboTipoDoc"));
		obj.setNumDocumento(request.getParameter("txtNroDoc"));
		obj.setCelular(request.getParameter("txtCelular"));
		obj.setEmail(request.getParameter("txtEmail"));
		obj.setContrasena(request.getParameter("txtContrasena"));
		obj.setIdCentroFormacion(Integer.parseInt(request.getParameter("cboCentro")));
		
		GestionRol rol = new GestionRol();
		obj.setRol(rol.buscarRol(Integer.parseInt(request.getParameter("cboRol"))));
		
		GestionPersonas objNeg = new GestionPersonas();
		
		try {
			Persona objPersona = objNeg.insertarPersona(obj);
			
			if(objPersona != null){				
				return new ModelAndView("mantenerUsuarios", "LISTA", objNeg.listarPersonas());
			} else {
				throw new Exception("Error al ingresar nuevo usuario.");
			}			
		} catch (DAOExcepcion e) {	
			return new ModelAndView("mantenerUsuarios", "ERROR", e.getMessage());			
		} catch (Exception e) {	
			return new ModelAndView("mantenerUsuarios", "ERROR", e.getMessage());
		}			
		
	}
	
	@RequestMapping(value = "/actualizarUsuario", method = RequestMethod.GET)	
	protected ModelAndView actualizarUsuario(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
				
		Persona obj = new Persona();
		
		obj.setIdPersona(Integer.parseInt(request.getParameter("id").trim()));
		obj.setNombre(request.getParameter("txtNombre"));
		obj.setApePaterno(request.getParameter("txtApePaterno"));
		obj.setApeMaterno(request.getParameter("txtApeMaterno"));
		obj.setSexo(request.getParameter("rbnSexo"));
		obj.setTipoDocumento(request.getParameter("cboTipoDoc"));
		obj.setNumDocumento(request.getParameter("txtNroDoc"));
		obj.setCelular(request.getParameter("txtCelular"));
		obj.setEmail(request.getParameter("txtEmail"));
		obj.setContrasena(request.getParameter("txtContrasena"));
		obj.setIdCentroFormacion(Integer.parseInt(request.getParameter("cboCentro")));
		
		GestionRol rol = new GestionRol();
		obj.setRol(rol.buscarRol(Integer.parseInt(request.getParameter("cboRol"))));
		
		GestionPersonas objNeg = new GestionPersonas();
		
		try {
			Persona objPersona = objNeg.actualizarPersona(obj);

			if(objPersona != null){				
				return new ModelAndView("mantenerUsuarios", "LISTA", objNeg.listarPersonas());
			} else {
				throw new Exception("Error al actualizar datos de usuario.\nDebe completar todos los campos correctamente.");
			}			
		} catch (DAOExcepcion e) {	
			return new ModelAndView("mantenerUsuarios", "ERROR", e.getMessage());			
		} catch (Exception e) {	
			return new ModelAndView("mantenerUsuarios", "ERROR", e.getMessage());
		}		
	}
		
	@RequestMapping(value = "/eliminarUsuario", method = RequestMethod.GET)	
	protected ModelAndView eliminarUsuario(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
				
		int id = Integer.parseInt(request.getParameter("id").trim());
				
		GestionPersonas objNeg = new GestionPersonas();
		
		try {
			int result = objNeg.eliminarPersona(id);

			if(result != 0){				
				return new ModelAndView("mantenerUsuarios", "LISTA", objNeg.listarPersonas());
			} else {
				throw new Exception("Error al eliminar usuario.");
			}			
		} catch (DAOExcepcion e) {	
			return new ModelAndView("mantenerUsuarios", "ERROR", e.getMessage());			
		} catch (Exception e) {	
			return new ModelAndView("mantenerUsuarios", "ERROR", e.getMessage());
		}		
	}
	
}
